package com.muy.redis.common.textfield;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.actionSystem.ex.CustomComponentAction;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.muy.redis.common.tree.TreePanelMark;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.function.Consumer;

/**
 * @Author jiyanghuang
 * @Date 2022/8/7 22:00
 */
public class SearchKeywordFunUpdateAction extends AnAction implements CustomComponentAction, DumbAware {

    private Consumer<String> search;

    private Project project;

    private MRSearchTextField searchTextField;

    protected TreePanelMark treePanelMark;

    public SearchKeywordFunUpdateAction(TreePanelMark treePanelMark){
        super("searchKeyword", "输入回车", AllIcons.Actions.Search);
        this.treePanelMark = treePanelMark;
    }

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        if(null == searchTextField || null == search){
            return;
        }
        search.accept(searchTextField.getText());
    }

    @Override
    public @NotNull JComponent createCustomComponent(@NotNull Presentation presentation, @NotNull String place) {
        return searchTextField;
    }

    public void setSearch(Consumer<String> search) {
        this.search = search;
    }

    public void setSearchTextField(MRSearchTextField searchTextField) {
        this.searchTextField = searchTextField;
    }

    public void updateSearchFun(Consumer<String> search){
        if(null == search){
            return;
        }

        this.search = search;
        searchTextField = new MRSearchTextField((value) ->{
            search.accept(value);
            return true;
        });
    }

    public String searchTextValue(){
        return searchTextField.getText();
    }
}
